package org.example.test.seven;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.example.test.util.RabbitMQUtils;

/**
 * @Author: 以默
 * @Date: 2024/7/18 15:48
 *  声明主题交换机 及 绑定
 *  消费者 C2
 */
public class ReceiveLogsTopic02 {
    public static final String EXCHANGE_NAME = "topic-logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        // 声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");
        System.out.println("等待接收消息.......");
        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("收到消息：" + new String(message.getBody(), "UTF-8"));
            // 手动确认消息
            System.out.println("接收队列" + queueName + "绑定键：" + message.getEnvelope().getRoutingKey());
        };
        channel.basicConsume(queueName, true, deliverCallback,consumerTag -> {});
    }


}
